package com.tech.connect.zhaorencai.zhuanwaikuai;

import android.os.Bundle;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.activity.SearchActivity;
import com.tech.connect.view.MenuTabView;

import java.util.ArrayList;
import java.util.List;

public class ZhuanWaiKuaiMainActivity extends BaseActivity implements View.OnClickListener {

    private int currentIndex = -1;
    private List<MenuTabView> tabViewList;
    private ZhuanWaiKuaiMainFragment zhuanWaiKuaiMainFragment;
    private TouBiaoRenFragment touBiaoRenFragment;
    private ZhongBiaoRenFragment zhongBiaoRenFragment;
    private BaoZhengJinFragment baoZhengJinFragment;
    private ZiJinTuoGuanFragment ziJinTuoGuanFragment;
    private ZuJiFragment zuJiFragment;

    @Override
    protected void initImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar
                .fitsSystemWindows(false)
                .keyboardEnable(true)
                .init();
    }

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_tab_main);
        initView();
    }

    private void initView() {
        MenuTabView tab0 = findViewById(R.id.tab0);
        MenuTabView tab1 = findViewById(R.id.tab1);
        MenuTabView tab2 = findViewById(R.id.tab2);
        MenuTabView tab3 = findViewById(R.id.tab3);
        MenuTabView tab4 = findViewById(R.id.tab4);
        tab0.setTab(MenuTabView.TabMenu.ZhuanWaiKuai_TouBiaoRen);
        tab1.setTab(MenuTabView.TabMenu.ZhuanWaiKuai_ZhongBiaoRen);
        tab2.setTab(MenuTabView.TabMenu.ZhuanWaiKuai_BaoZhengJin);
        tab3.setTab(MenuTabView.TabMenu.ZhuanWaiKuai_ZiJinTuoGuan);
        tab4.setTab(MenuTabView.TabMenu.ZhuanWaiKuai_ZuJi);
        tabViewList = new ArrayList<>();
        tabViewList.add(tab0);
        tabViewList.add(tab1);
        tabViewList.add(tab2);
        tabViewList.add(tab3);
        tabViewList.add(tab4);
        for (int i = 0; i < tabViewList.size(); i++) {
            MenuTabView menuTabView = tabViewList.get(i);
            menuTabView.setTag(i);
            menuTabView.setOnClickListener(this);
        }
        showFragment(-1);
    }

    @Override
    public void onBackPressed() {
        if (currentIndex == -1) {
            super.onBackPressed();
        } else {
            currentIndex = -1;
            for (int i = 0; i < tabViewList.size(); i++) {
                MenuTabView menuTabView = tabViewList.get(i);
                menuTabView.setSelected(false);
            }
            showFragment(currentIndex);
        }
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag != null && tag instanceof Integer) {
            int index = (int) tag;
            if (currentIndex == index) {
                return;
            }
            currentIndex = index;
            for (int i = 0; i < tabViewList.size(); i++) {
                MenuTabView menuTabView = tabViewList.get(i);
                if (currentIndex == i) {
                    menuTabView.setSelected(true);
                } else {
                    menuTabView.setSelected(false);
                }
            }
            //showFragment
            showFragment(currentIndex);

        }
    }

    private void showFragment(int index) {
        if (index == -1) {
            getHeadBar().setTitle("赚外快").setRightIcon(R.drawable.icon_search, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jump2Activity(SearchActivity.class);
                }
            });
            if (zhuanWaiKuaiMainFragment == null) {
                zhuanWaiKuaiMainFragment = new ZhuanWaiKuaiMainFragment();
                addFragment(R.id.container, zhuanWaiKuaiMainFragment);
            }
            showFragment(zhuanWaiKuaiMainFragment);
            hideFragment(touBiaoRenFragment);
            hideFragment(zhongBiaoRenFragment);
            hideFragment(baoZhengJinFragment);
            hideFragment(ziJinTuoGuanFragment);
            hideFragment(zuJiFragment);
        } else if (index == 0) {
            getHeadBar().setTitle("抢单人").clearRight();
            if (touBiaoRenFragment == null) {
                touBiaoRenFragment = new TouBiaoRenFragment();
                addFragment(R.id.container, touBiaoRenFragment);
            }
            hideFragment(zhuanWaiKuaiMainFragment);
            showFragment(touBiaoRenFragment);
            hideFragment(zhongBiaoRenFragment);
            hideFragment(baoZhengJinFragment);
            hideFragment(ziJinTuoGuanFragment);
            hideFragment(zuJiFragment);
        } else if (index == 1) {
            getHeadBar().setTitle("中单人").clearRight();
            if (zhongBiaoRenFragment == null) {
                zhongBiaoRenFragment = new ZhongBiaoRenFragment();
                addFragment(R.id.container, zhongBiaoRenFragment);
            }
            hideFragment(zhuanWaiKuaiMainFragment);
            hideFragment(touBiaoRenFragment);
            showFragment(zhongBiaoRenFragment);
            hideFragment(baoZhengJinFragment);
            hideFragment(ziJinTuoGuanFragment);
            hideFragment(zuJiFragment);
        } else if (index == 2) {
            getHeadBar().setTitle("保证金").clearRight();
            if (baoZhengJinFragment == null) {
                baoZhengJinFragment = new BaoZhengJinFragment();
                addFragment(R.id.container, baoZhengJinFragment);
            }
            hideFragment(zhuanWaiKuaiMainFragment);
            hideFragment(touBiaoRenFragment);
            hideFragment(zhongBiaoRenFragment);
            showFragment(baoZhengJinFragment);
            hideFragment(ziJinTuoGuanFragment);
            hideFragment(zuJiFragment);
        } else if (index == 3) {
            getHeadBar().setTitle("赏金托管").clearRight();
            if (ziJinTuoGuanFragment == null) {
                ziJinTuoGuanFragment = new ZiJinTuoGuanFragment();
                addFragment(R.id.container, ziJinTuoGuanFragment);
            }
            hideFragment(zhuanWaiKuaiMainFragment);
            hideFragment(touBiaoRenFragment);
            hideFragment(zhongBiaoRenFragment);
            hideFragment(baoZhengJinFragment);
            showFragment(ziJinTuoGuanFragment);
            hideFragment(zuJiFragment);
        } else if (index == 4) {
            getHeadBar().setTitle("足迹").clearRight();
            if (zuJiFragment == null) {
                zuJiFragment = new ZuJiFragment();
                addFragment(R.id.container, zuJiFragment);
            }
            hideFragment(zhuanWaiKuaiMainFragment);
            hideFragment(touBiaoRenFragment);
            hideFragment(baoZhengJinFragment);
            hideFragment(zhongBiaoRenFragment);
            hideFragment(ziJinTuoGuanFragment);
            showFragment(zuJiFragment);
        }
    }

}
